const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const helmet = require('helmet');
const compression = require('compression');
const dotenv = require('dotenv');
const routes = require('./routes');
const errorHandler = require('./middleware/errorHandler');
const responseMiddleware = require('./middleware/response');

dotenv.config();
const app = express();

// 安全增强中间件
app.use(helmet());
app.use(cors());

// 日志和性能优化
app.use(morgan('dev'));
app.use(compression());

// 解析中间件
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 使用响应中间件
app.use(responseMiddleware);

// 挂载路由
app.use('/api', routes);

// 错误处理
app.use(errorHandler);

module.exports = app;